using Items;
using Services;
using System.Collections.Generic;
using UnityEngine;

public class EquipmentPanel : MonoBehaviour
{
    private EquipmentManager equipmentManager;
    private readonly Dictionary<EEqiupPart, EquipmentUI> ui_equipments = new Dictionary<EEqiupPart, EquipmentUI>();
    private readonly Dictionary<ESect, EquipmentUI> ui_weapons = new Dictionary<ESect, EquipmentUI>();

    private void Awake()
    {
        EquipmentUI[] tmp_equipments = GetComponentsInChildren<EquipmentUI>();
        foreach (EquipmentUI equipment in tmp_equipments)
        {
            if (equipment.Part == EEqiupPart.Hand)
                ui_weapons.Add(equipment.Sect, equipment);
            else
                ui_equipments.Add(equipment.Part, equipment);
        }
    }

    private void Start()
    {
        equipmentManager = ServiceLocator.Get<ActionController>().player.EquipmentManager;
        equipmentManager.EquipmentChange += Show;
    }

    private void Show()
    {
        foreach (EEqiupPart part in ui_equipments.Keys)
        {
            equipmentManager.Equipments.TryGetValue(part, out Equipment equipment);
            ui_equipments[part].Show(equipment);
        }
        foreach (ESect sect in ui_weapons.Keys)
        {
            equipmentManager.SpareWeapons.TryGetValue(sect, out Equipment equipment);
            ui_weapons[sect].Show(equipment);
        }
    }
}
